/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using System;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PageAutoGratuityImp : PageImpBaseClass
    {
        ComboBox comboBoxName;
        GroupBox mainGroupBox;

        const int CONST_DEFAULT_MAG_STRIPE_DELAY = 700;

        /// <summary>
        /// Constructor
        /// </summary>
        public PageAutoGratuityImp(UIElementCollection stackControlsChild,
            GroupBox mainGroupBox,
            ComboBox cBoxName,
            DbLinkedTextBox tbName,
            DbLinkedTextBox tbRate,
            DbLinkedTextBox tbSeatStartCount,
            DbLinkedTextBox tbAboveTicketAmount,
            Button bMon,
            Button bTues,
            Button bWed,
            Button bThur,
            Button bFri,
            Button bSat,
            Button bSun,
            Button bSetAllTimes)
        {

            bMon.Click += new System.Windows.RoutedEventHandler(bMon_Click);
            bTues.Click += new System.Windows.RoutedEventHandler(bTues_Click);
            bWed.Click += new System.Windows.RoutedEventHandler(bWed_Click);
            bThur.Click += new System.Windows.RoutedEventHandler(bThur_Click);
            bFri.Click += new System.Windows.RoutedEventHandler(bFri_Click);
            bSat.Click += new System.Windows.RoutedEventHandler(bSat_Click);
            bSun.Click += new System.Windows.RoutedEventHandler(bSun_Click);
            bSetAllTimes.Click += new System.Windows.RoutedEventHandler(bSetAllTimes_Click);

            mListOfControlsToHideInNonEditMode.Add(bMon);
            mListOfControlsToHideInNonEditMode.Add(bTues);
            mListOfControlsToHideInNonEditMode.Add(bWed);
            mListOfControlsToHideInNonEditMode.Add(bThur);
            mListOfControlsToHideInNonEditMode.Add(bFri);
            mListOfControlsToHideInNonEditMode.Add(bSat);
            mListOfControlsToHideInNonEditMode.Add(bSun);

            mNewGroupIsDisabled = true;
            mDeleteGroupIsDisabled = true;
            mButtonNewEntityName = "New Auto Gratuity";
            mButtonDeleteEntityName = "Delete Auto Gratuity";

            this.ButtonNumber = "12";
            this.ButtonLabelText = "Auto Gratuity";
            this.mainGroupBox = mainGroupBox;
            this.comboBoxName = cBoxName;
            RegisterSaveMeGroupBoxes(null, mainGroupBox);

            ///Register the controls and lock the form.
            RegisterUiElementsList(stackControlsChild);

            ///Add all text boxes to the list.
            mTextBoxesList.Add(tbSeatStartCount);
            mTextBoxesList.Add(tbRate);
            mTextBoxesList.Add(tbAboveTicketAmount);
            mTextBoxesList.Add(tbName);

            ///Add the combo boxes.
            ListofComboboxes.Add(comboBoxName);


            mEntityAddDeleteLoggingEnabled = true;
        }

        void bSetAllTimes_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                AUTO_GRATUITY selected = comboBoxName.SelectedItem as AUTO_GRATUITY;
                if (selected != null)
                {
                    TimePicker dtp = new TimePicker(true);
                    dtp.ShowDialog();
                    if (TimePicker.LastResponse == UserResponse.YES)
                    {
                        selected.MondayStartTime = TimePicker.LastStartSelectedDateTime;
                        selected.MondayEndTime = TimePicker.LastEndSelectedDateTime;
                        selected.TuesdayStartTime = TimePicker.LastStartSelectedDateTime;
                        selected.TuesdayEndTime = TimePicker.LastEndSelectedDateTime;
                        selected.WednesdayStartTime = TimePicker.LastStartSelectedDateTime;
                        selected.WednesdayEndTime = TimePicker.LastEndSelectedDateTime;
                        selected.ThursdayStartTime = TimePicker.LastStartSelectedDateTime;
                        selected.ThursdayEndTime = TimePicker.LastEndSelectedDateTime;
                        selected.FridayStartTime = TimePicker.LastStartSelectedDateTime;
                        selected.FridayEndTime = TimePicker.LastEndSelectedDateTime;
                        selected.SaturdayStartTime = TimePicker.LastStartSelectedDateTime;
                        selected.SaturdayEndTime = TimePicker.LastEndSelectedDateTime;
                        selected.SundayStartTime = TimePicker.LastStartSelectedDateTime;
                        selected.SundayEndTime = TimePicker.LastEndSelectedDateTime;
                        Context.SaveChanges();
                    }
                }
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_REPORT_ERROR, exc);
            }
        }

        void SetDateTime(int weekDay)
        {
            try
            {
                AUTO_GRATUITY selected = comboBoxName.SelectedItem as AUTO_GRATUITY;
                if (selected != null)
                {
                    TimePicker dtp = new TimePicker(true);
                    dtp.ShowDialog();
                    if (TimePicker.LastResponse == UserResponse.YES)
                    {
                        if (weekDay == 1)
                        {
                            selected.MondayStartTime = TimePicker.LastStartSelectedDateTime;
                            selected.MondayEndTime = TimePicker.LastEndSelectedDateTime;
                        }
                        else if (weekDay == 2)
                        {
                            selected.TuesdayStartTime = TimePicker.LastStartSelectedDateTime;
                            selected.TuesdayEndTime = TimePicker.LastEndSelectedDateTime;
                        }
                        else if (weekDay == 3)
                        {
                            selected.WednesdayStartTime = TimePicker.LastStartSelectedDateTime;
                            selected.WednesdayEndTime = TimePicker.LastEndSelectedDateTime;
                        }
                        else if (weekDay == 4)
                        {
                            selected.ThursdayStartTime = TimePicker.LastStartSelectedDateTime;
                            selected.ThursdayEndTime = TimePicker.LastEndSelectedDateTime;
                        }
                        else if (weekDay == 5)
                        {
                            selected.FridayStartTime = TimePicker.LastStartSelectedDateTime;
                            selected.FridayEndTime = TimePicker.LastEndSelectedDateTime;
                        }
                        else if (weekDay == 6)
                        {
                            selected.SaturdayStartTime = TimePicker.LastStartSelectedDateTime;
                            selected.SaturdayEndTime = TimePicker.LastEndSelectedDateTime;
                        }
                        else if (weekDay == 7)
                        {
                            selected.SundayStartTime = TimePicker.LastStartSelectedDateTime;
                            selected.SundayEndTime = TimePicker.LastEndSelectedDateTime;
                        }
                        Context.SaveChanges();
                    }
                }
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
        }

        void bSun_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetDateTime(7);
        }

        void bSat_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetDateTime(6);
        }

        void bFri_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetDateTime(5);
        }

        void bThur_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetDateTime(4);
        }

        void bWed_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetDateTime(3);
        }

        void bTues_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetDateTime(2);
        }

        void bMon_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SetDateTime(1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetEntityObjectSet()
        {
            var q = from AUTO_GRATUITY in Context.AUTO_GRATUITYS
                    where AUTO_GRATUITY.IsDeleted == false
                    select AUTO_GRATUITY;
            if (q.Any())
            {
                return new List<EntityObject>(q.ToList());
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void MarkEntityDeleted()
        {
            AUTO_GRATUITY selected = comboBoxName.SelectedItem as AUTO_GRATUITY;
            if (selected != null)
            {
                selected.IsDeleted = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        override public void ResetFocus()
        {
            base.ResetFocus();
            mainGroupBox.DataContext = null;
            mainGroupBox.DataContext = Context;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedEntityObject()
        {
            AUTO_GRATUITY selected = comboBoxName.SelectedItem as AUTO_GRATUITY;
            return selected;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersEntitySource()
        {
            ///Create the query object.
            var query = from AUTO_GRATUITY in Context.AUTO_GRATUITYS
                        where AUTO_GRATUITY.IsDeleted == false
                        orderby AUTO_GRATUITY.Name
                        select AUTO_GRATUITY;

            if (query.Any())
            {
                comboBoxName.ItemsSource = query;
                comboBoxName.SelectedItem = query.First();
            }
            else
            {
                comboBoxName.ItemsSource = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject AddNewEntityObject()
        {
            AUTO_GRATUITY newObject = null;
            var q = from AUTO_GRATUITY in Context.AUTO_GRATUITYS
                    where AUTO_GRATUITY.Name.Equals("Not Assigned")
                    select AUTO_GRATUITY;

            if (!q.Any())
            {
                newObject = new AUTO_GRATUITY();
                newObject.Name = "Not Assigned";

                newObject.MondayStartTime = DateTime.Now;
                newObject.MondayEndTime = DateTime.Now;

                newObject.TuesdayStartTime = DateTime.Now;
                newObject.TuesdayEndTime = DateTime.Now;

                newObject.WednesdayStartTime = DateTime.Now;
                newObject.WednesdayEndTime = DateTime.Now;

                newObject.ThursdayStartTime = DateTime.Now;
                newObject.ThursdayEndTime = DateTime.Now;

                newObject.FridayStartTime = DateTime.Now;
                newObject.FridayEndTime = DateTime.Now;

                newObject.SaturdayStartTime = DateTime.Now;
                newObject.SaturdayEndTime = DateTime.Now;

                newObject.SundayStartTime = DateTime.Now;
                newObject.SundayEndTime = DateTime.Now;

                Context.AUTO_GRATUITYS.AddObject(newObject);
            }
            else
            {
                newObject = q.First();
                newObject.IsDeleted = false;
            }

            return newObject;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetEntityComboBox()
        {
            return comboBoxName;
        }
    }
}
